package com.czwj.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.czwj.annotation.SystemControllerLog;
import com.czwj.entities.BudgetExecute;
import com.czwj.entities.BudgetMain;
import com.czwj.entities.DataGridData;
import com.czwj.entities.Form;
import com.czwj.entities.OperateResult;
import com.czwj.entities.PageBean;
import com.czwj.entities.ProjectItem;
import com.czwj.entities.UserInfo;
import com.czwj.service.BudgetExecuteService;
import com.czwj.service.FormService;
import com.czwj.service.ProjectItemService;
import com.czwj.util.TextUtil;
import com.czwj.util.WechatUtil;

import net.sf.json.JSONObject;

@Controller
public class BudgetExecuteAction {

	@Resource
	BudgetExecuteService budgetExecuteService;
	@Resource
	ProjectItemService projectItemService;
	@Resource
	FormService formService;
	
	@Value("${review_model_code}")
	private String model_code;
	
	SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");
	
	@RequestMapping(value = "/level3/budgetexecute.do", method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<BudgetExecute> budgetMain(@RequestParam(value = "page", required = false) String page,
			@RequestParam(value = "rows", required = false) String rows,
			@RequestParam(value = "projectitemid", required = false) String projectitemid,
			@RequestParam(value = "status", required = false) String status) {
		DataGridData<BudgetExecute> result = new DataGridData<>();
		Map<String, Object> map = new HashMap<String, Object>();
		PageBean pageBean = null;
		if (TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)) {
			pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
		}
		if (pageBean != null) {
			map.put("start", pageBean.getStart());
			map.put("size", pageBean.getPageSize());
		}
		if(TextUtil.isNotEmpty(projectitemid)&&!projectitemid.equals("0")){
			map.put("projectitemid", projectitemid);
		}
		if(TextUtil.isNotEmpty(status)){
			map.put("status", status);
		}
		Subject currentUser = SecurityUtils.getSubject();
		map.put("userid", ((UserInfo)currentUser.getSession().getAttribute("userInfo")).getId());
		List<BudgetExecute> list = budgetExecuteService.searchbylevel3(map);
		result.setRows(list);
		result.setTotal(budgetExecuteService.countbylevel3(map));
		return result;
	}
	
	@SystemControllerLog(description = "修改预算执行") 
	@RequestMapping(value = "/budgetExecuteSave.do", method = RequestMethod.POST)
	@ResponseBody
	public OperateResult budgetExecuteSave(@RequestParam(value = "images", required = false) MultipartFile[] images,
			@RequestParam(value = "id", required = false) Integer id,
			@RequestParam(value = "itemid", required = false) Integer itemid,
			@RequestParam(value = "projectitemid", required = false) Integer projectitemid,
			@RequestParam(value = "projectitemchildid", required = false) Integer projectitemchildid,
			@RequestParam(value = "executedate", required = false) String executedate,
			@RequestParam(value = "money", required = false) String money,
			@RequestParam(value = "content", required = false) String content,
			@RequestParam(value = "status", required = false) Integer status,HttpServletRequest request){
		OperateResult result = new OperateResult();
		int count = 0;
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo creater = ((UserInfo) currentUser.getSession().getAttribute("userInfo"));
		String createip = request.getRemoteAddr();
		count = budgetExecuteService.insertorupdate(id,itemid,projectitemid,projectitemchildid,executedate,money,content,status,creater.getId(),createip,images);
		ProjectItem projectItem = projectItemService.selectByPrimaryKey(projectitemid);
		for (UserInfo user : projectItem.getLevel2()) {
			JSONObject data = WechatUtil.setReviewData(projectItem.getName()+" 预算执行", sdf2.format(new Date()));
			sendReview(user, data);
		}
		result.setSuccess(count>0);
		return result;
	}
	
	@SystemControllerLog(description = "删除预算执行") 
	@RequestMapping(value = "budgetExecuteRemove.do", method = RequestMethod.POST)
	@ResponseBody
	public OperateResult removeBudgetExecute(@RequestParam("id") int id) {
		OperateResult result = new OperateResult();
		try {
			int count = budgetExecuteService.remove(id);
			result.setSuccess(count > 0);
			return result;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			result.setSuccess(false);
			return result;
		}
	}
	
	@RequestMapping(value = "/review/budgetexcute.do", method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<BudgetMain> reviewBudgetExcute(@RequestParam(value = "page", required = false) String page,
			@RequestParam(value = "rows", required = false) String rows,
			@RequestParam(value = "startdate", required = false) String startdate,
			@RequestParam(value = "enddate", required = false) String enddate,
			@RequestParam(value = "projectitemid", required = false) String projectitemid) {
		DataGridData<BudgetMain> result = new DataGridData();
		Map<String, Object> map = new HashMap<String, Object>();
		PageBean pageBean = null;
		if (TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)) {
			pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
		}
		if (pageBean != null) {
			map.put("start", pageBean.getStart());
			map.put("size", pageBean.getPageSize());
		}
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		map.put("userid", userInfo.getId());
		if(TextUtil.isNotEmpty(startdate)){
			map.put("startdate", startdate);
		}
		if(TextUtil.isNotEmpty(enddate)){
			map.put("enddate", enddate);
		}
		if(TextUtil.isNotEmpty(projectitemid)&&!projectitemid.equals("0")){
			map.put("projectitemid", projectitemid);
		}
		int roleid = userInfo.getRoleid();
		if(roleid ==1 ||roleid ==2){
			map.put("status", 7);
			result.setRows(budgetExecuteService.searchbylevel1(map));
			result.setTotal(budgetExecuteService.countbylevel1(map));
		}
		if(roleid==3){
			map.put("status", 6);
			result.setRows(budgetExecuteService.searchbylevel2(map));
			result.setTotal(budgetExecuteService.countbylevel2(map));
		}
		
		return result;
	}
	
	@RequestMapping(value = "/reviewed/budgetexcute.do", method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<BudgetMain> reviewedBudgetExcute(@RequestParam(value = "page", required = false) String page,
			@RequestParam(value = "rows", required = false) String rows,
			@RequestParam(value = "startdate", required = false) String startdate,
			@RequestParam(value = "enddate", required = false) String enddate,
			@RequestParam(value = "projectitemid", required = false) String projectitemid) {
		DataGridData<BudgetMain> result = new DataGridData();
		Map<String, Object> map = new HashMap<String, Object>();
		PageBean pageBean = null;
		if (TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)) {
			pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
		}
		if (pageBean != null) {
			map.put("start", pageBean.getStart());
			map.put("size", pageBean.getPageSize());
		}
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		map.put("userid", userInfo.getId());
		if(TextUtil.isNotEmpty(startdate)){
			map.put("startdate", startdate);
		}
		if(TextUtil.isNotEmpty(enddate)){
			map.put("enddate", enddate);
		}
		if(TextUtil.isNotEmpty(projectitemid)&&!projectitemid.equals("0")){
			map.put("projectitemid", projectitemid);
		}
		int roleid = userInfo.getRoleid();
		if(roleid ==1 ||roleid ==2){
			map.put("status", 4);
			result.setRows(budgetExecuteService.searchbylevel1(map));
			result.setTotal(budgetExecuteService.countbylevel1(map));
		}
		if(roleid==3){
			map.put("status", "4,7");
			result.setRows(budgetExecuteService.searchbylevel2(map));
			result.setTotal(budgetExecuteService.countbylevel2(map));
		}
		
		return result;
	}
	
	@RequestMapping(value = "/budgetExecuteReviewed.do", method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<BudgetExecute> budgetExecuteReviewed(@RequestParam(value = "page", required = false) String page,
			@RequestParam(value = "rows", required = false) String rows) {
		DataGridData<BudgetExecute> result = new DataGridData();
		Map<String, Object> map = new HashMap<String, Object>();
		PageBean pageBean = null;
		if (TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)) {
			pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
		}
		if (pageBean != null) {
			map.put("start", pageBean.getStart());
			map.put("size", pageBean.getPageSize());
		}
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		map.put("userid", userInfo.getId());
		if (userInfo.getRoleid()==1||userInfo.getRoleid()==2) {
			map.put("status", "3,4");
			result.setRows(budgetExecuteService.searchReviewedLevel1(map));
			result.setTotal(budgetExecuteService.countReviewedLevel1(map));
		}else if (userInfo.getRoleid()==3) {
			map.put("status", "3,4,7");
			result.setRows(budgetExecuteService.searchReviewedLevel2(map));
			result.setTotal(budgetExecuteService.countReviewedLevel2(map));
		}
		return result;
	}
	
	
	
	@RequestMapping(value = "/budgetexecute/search.do", method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<BudgetExecute> search(@RequestParam(value = "page", required = false) String page,
			@RequestParam(value = "rows", required = false) String rows,
			@RequestParam(value = "projectitemid", required = false) String projectitemid,
			@RequestParam(value = "projectitemchildid", required = false) String projectitemchildid,
			@RequestParam(value = "status", required = false) String status) {
		DataGridData<BudgetExecute> result = new DataGridData<>();
		Map<String, Object> map = new HashMap<String, Object>();
		PageBean pageBean = null;
		if (TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)) {
			pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
		}
		if (pageBean != null) {
			map.put("start", pageBean.getStart());
			map.put("size", pageBean.getPageSize());
		}
		if(TextUtil.isNotEmpty(projectitemid)&&!projectitemid.equals("0")){
			map.put("projectitemid", projectitemid);
		}
		if(TextUtil.isNotEmpty(projectitemchildid)&&!projectitemchildid.equals("0")){
			map.put("projectitemchildid", projectitemchildid);
		}
		if(TextUtil.isNotEmpty(status)){
			map.put("status", status);
		}
		List<BudgetExecute> list = budgetExecuteService.search(map);
		result.setRows(list);
		result.setTotal(budgetExecuteService.count(map));
		return result;
	}
	
	@RequestMapping(value = "/getexecute.do", method = RequestMethod.GET)
	@ResponseBody
	public List<BudgetExecute> getexecute(@RequestParam(value = "projectitemid", required = true) int projectitemid,
			@RequestParam(value = "itemid", required = true) int itemid,
			@RequestParam(value = "status", required = true) String status) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("projectitemid", projectitemid);
		map.put("itemid", itemid);
		map.put("status", status);
		List<BudgetExecute> list = budgetExecuteService.getexecute(map);
		return list;
	}
	
	@RequestMapping(value = "/getUnreviewedExecuteMoneySituation", method = RequestMethod.GET)
	@ResponseBody
	public double moneyposition(@RequestParam(value = "projectitemid") int projectitemid,
			@RequestParam(value = "itemid") int budgetitemid,
			@RequestParam(value = "executeid") Integer executeid) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("projectitemid", projectitemid);
		map.put("itemid", budgetitemid);
		if(executeid!=null){
			map.put("executeid", executeid);
		}
		double result = budgetExecuteService.getUnreviewedExecuteMoneySituation(map);
		return result;
	}

	@RequestMapping(value = "/getUnreviewedExecuteMoneySituationchild", method = RequestMethod.GET)
	@ResponseBody
	public double getUnreviewedExecuteMoneySituationchild(@RequestParam(value = "projectitemchildid") int projectitemchildid,
			@RequestParam(value = "itemid") int budgetitemid,
			@RequestParam(value = "executeid") Integer executeid) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("projectitemchildid", projectitemchildid);
		map.put("itemid", budgetitemid);
		if(executeid!=null){
			map.put("executeid", executeid);
		}
		double result = budgetExecuteService.getUnreviewedExecuteMoneySituationchild(map);
		return result;
	}
	
	private void sendReview(UserInfo userInfo,JSONObject data){
		List<Form> forms = formService.findbyuser(userInfo.getId());
		for (Form form : forms) {
			String code = WechatUtil.pushMessage(userInfo.getOpenid(), model_code, form.getFormid(),data);
			formService.deleteByPrimaryKey(form.getId());
			if(code.trim().equals("0")){
				return;
			}
		}
	}
}
